package lance.honeydew.dao;

import java.util.ArrayList;
import java.util.List;

import lance.honeydew.bean.CategoryBig;
import lance.honeydew.bean.ItemStock;
import lance.honeydew.bean.ShoppingMoney;
import lance.honeydew.bean.UserMember;
import lance.honeydew.service.ManageService;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;

public class UserMemberDAO extends RootDAO {
	
	private static final Logger logger = Logger.getLogger(UserMemberDAO.class);
	
	/**
	 * queryUserMemberByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<UserMember> queryUserMemberByQb(UserMember qb, int pageNum, int pageSize) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(UserMember.class);
		List<UserMember> list = null;
		
		try {
			
			if (StringUtils.isNotEmpty(qb.getEmail())) {
				criteria.add(Restrictions.eq(UserMember.PROP_EMAIL, qb.getEmail()));
			}
			
			if (StringUtils.isNotEmpty(qb.getName())) {
				criteria.add(Restrictions.eq(UserMember.PROP_NAME, qb.getName()));
			}
			
			if (StringUtils.isNotEmpty(qb.getHandphone())) {
				criteria.add(Restrictions.eq(UserMember.PROP_HANDPHONE, qb.getHandphone()));
			}
			
			if (StringUtils.isNotEmpty(qb.getLocalphone())) {
				criteria.add(Restrictions.eq(UserMember.PROP_LOCALPHONE, qb.getLocalphone()));
			}
			
			if (StringUtils.isNotEmpty(qb.getAddress())) {
				criteria.add(Restrictions.eq(UserMember.PROP_ADDRESS, qb.getAddress()));
			}
			
			if (qb.getIdentity() != null) {
				criteria.add(Restrictions.eq(UserMember.PROP_IDENTITY, qb.getIdentity()));
			}
			
			list = (List<UserMember>) this.getHibernateTemplate()
					.findByCriteria(criteria);
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return list;
	}
	
	/**
	 * findUserMemberByUserId
	 * @param obj
	 * @return
	 */
	public UserMember findUserMemberByUserId(UserMember obj) {
		
		UserMember result = new UserMember();
		DetachedCriteria criteria = DetachedCriteria.forClass(UserMember.class);
		
		try {
			
			criteria.add(Restrictions.eq(UserMember.PROP_USERID, obj.getUserid()));
			
			result = (UserMember) this.getHibernateTemplate().findByCriteria(criteria).get(0);
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateUserMemberByObj
	 * @param obj
	 * @return
	 */
	public boolean updateUserMemberByObj(UserMember obj) {
		
		boolean result = false;
		
		try {
			this.update(obj);
			
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * deleteUserMemberByUserId
	 * @param userId
	 * @return
	 */
	public boolean deleteUserMemberByUserId(Long userId) {
		
		boolean result = false;
		
		try {
			
			UserMember obj = new UserMember(userId);
			
			if (obj != null){
				
				this.delete(UserMember.PROP_USERID, obj);
		        
				result = true;
			}
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * checkLoginUserMember
	 * @param email
	 * @param password
	 * @return
	 */
	public UserMember checkLoginUserMember(String email, String password) {
		
		UserMember user = null;
		
		try {
			
			if (StringUtils.isNotEmpty(email) && 
					StringUtils.isNotEmpty(password)) {
				
				DetachedCriteria criteria = DetachedCriteria.forClass(UserMember.class);
				criteria.add(Restrictions.eq(UserMember.PROP_EMAIL, email));
				criteria.add(Restrictions.eq(UserMember.PROP_PASSWORD, password));
				
				criteria.setProjection(Projections.projectionList()
						.add(Projections.property(UserMember.PROP_USERID), "_userid")
						.add(Projections.property(UserMember.PROP_NAME), "_name")
						.add(Projections.property(UserMember.PROP_EMAIL), "_email")
						.add(Projections.property(UserMember.PROP_PASSWORD), "_password")
						.add(Projections.property(UserMember.PROP_ADDRESS), "_address")
						.add(Projections.property(UserMember.PROP_HANDPHONE), "_handphone")
						.add(Projections.property(UserMember.PROP_LOCALPHONE), "_localphone")
						.add(Projections.property(UserMember.PROP_STATE), "_state")
						.add(Projections.property(UserMember.PROP_IDENTITY), "_identity")
						.add(Projections.property(UserMember.PROP_TOTAL_SHOPPING_MONEY), "_totalShoppingMoney")
						);
				criteria.setResultTransformer(Transformers.aliasToBean(UserMember.class));
				
				user = (UserMember) this.getHibernateTemplate().
						findByCriteria(criteria).get(0);
				
			}			
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return user;
	}
	
	/**
	 * queryShoppingMoneyByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<ShoppingMoney> queryShoppingMoneyByQb(UserMember qb, int pageNum, int pageSize) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(UserMember.class);
		List<ShoppingMoney> list = null;
		
		try {
			
			criteria.add(Restrictions.eq(UserMember.PROP_EMAIL, qb.getEmail()));
			criteria.add(Restrictions.eq(UserMember.PROP_NAME, qb.getName()));
			
			UserMember user = (UserMember) this.getHibernateTemplate()
					.findByCriteria(criteria).get(0);
			
			if (user != null) {
				list = new ArrayList<ShoppingMoney>(user.getShoppingMoneySet());
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return list;
	}
	
	/**
	 * findUserMemberByEmailAndName
	 * @param bo
	 * @return
	 */
	public UserMember findUserMemberByEmailAndName(UserMember bo) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(UserMember.class);
		UserMember result = null;
		
		try {
			
			criteria.add(Restrictions.eq(UserMember.PROP_EMAIL, bo.getEmail()));
			criteria.add(Restrictions.eq(UserMember.PROP_NAME, bo.getName()));
			result = (UserMember) this.getHibernateTemplate()
					.findByCriteria(criteria).get(0);
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findUserMemberByEmail
	 * @param email
	 * @return
	 */
	public UserMember findUserMemberByEmail(String email) {
		
		UserMember user = null;
		
		try {
			
			DetachedCriteria criteria = DetachedCriteria.forClass(UserMember.class);
			criteria.add(Restrictions.eq(UserMember.PROP_EMAIL, email));
			
			criteria.setProjection(Projections.projectionList()
					.add(Projections.property(UserMember.PROP_USERID), "_userid")
					.add(Projections.property(UserMember.PROP_NAME), "_name")
					.add(Projections.property(UserMember.PROP_EMAIL), "_email")
					.add(Projections.property(UserMember.PROP_PASSWORD), "_password")
					.add(Projections.property(UserMember.PROP_ADDRESS), "_address")
					.add(Projections.property(UserMember.PROP_HANDPHONE), "_handphone")
					.add(Projections.property(UserMember.PROP_LOCALPHONE), "_localphone")
					.add(Projections.property(UserMember.PROP_STATE), "_state")
					.add(Projections.property(UserMember.PROP_IDENTITY), "_identity")
					.add(Projections.property(UserMember.PROP_TOTAL_SHOPPING_MONEY), "_totalShoppingMoney")
					);
			criteria.setResultTransformer(Transformers.aliasToBean(UserMember.class));
			
			user = (UserMember) this.getHibernateTemplate().
					findByCriteria(criteria).get(0);			
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return user;
	}
}